//
//  DTCMQTTManager.m
//  MQTTDemo
//
//  Created by JianWei Chen on 2017/11/22.
//  Copyright © 2017年 JianWei Chen. All rights reserved.
//
#import "MQTTSession.h"
#import "DTCMQTTManager.h"
#import "MQTTCFSocketTransport.h"

@interface DTCMQTTManager ()<MQTTSessionDelegate>

/**MQTT 客户端*/
@property (nonatomic, strong) MQTTSession *session;
/**通讯*/
@property (nonatomic, strong) MQTTCFSocketTransport *transport;
/**代理*/
@property (nonatomic, weak) id <DTCMQTTManagerDelegate> delegate;

@end

@implementation DTCMQTTManager

#pragma mark - 事件处理
- (void)connectToHost:(NSString *)hostString port:(int)port
{
    self.transport.host = hostString;
    self.transport.port = port;
    self.session.delegate = self;
    [self.session connect];
    
    __weak DTCMQTTManager *weakSelf = self;
    self.session.messageHandler = ^(NSData *message, NSString *topic) {
        
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(mqttManager:messageBack:topic:)]) {
            [weakSelf.delegate mqttManager:weakSelf messageBack:message topic:topic];
        }
    };
}

- (void)disconnect
{
    [self.session disconnect];
}

- (void)subscribeSubject:(NSString *)topic
{
    [self.session subscribeToTopic:topic atLevel:MQTTQosLevelAtMostOnce subscribeHandler:^(NSError *error, NSArray<NSNumber *> *gQoss) {
        if (error) {
            NSLog(@"MQTT 主题订阅：%@ 错误:%@",topic,error);
        }else{
            NSLog(@"MQTT 主题订阅:%@ 成功",topic);
        }
    }];
}

- (void)connectionError:(NSError *)error
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(mqttManager:connectResult:)]) {
        [self.delegate mqttManager:self connectResult:error];
    }
    
    if (error)
    {
        NSLog(@"MQTT 连接断开，原因：%@",error);
    }
    else
    {
        NSLog(@"MQTT 连接成功");
    }
}
#pragma mark - 发送数据
- (void)publishData:(NSData *)data onTopic:(NSString *)topic
{
    [self.session publishData:data onTopic:topic retain:NO qos:MQTTQosLevelAtMostOnce];
}


#pragma mark - 代理
#pragma mark - <MQTTSessionDelegate>
- (void)connected:(MQTTSession *)session
{
    [self connectionError:nil];
}

- (void)connectionError:(MQTTSession *)session error:(NSError *)error
{
    [self connectionError:error];
}

- (void)connectionRefused:(MQTTSession *)session error:(NSError *)error
{
    [self connectionError:error];
}

#pragma mark - 通知处理


#pragma mark - 初始化
+ (id)shareManagerWithDeleagate:(id<DTCMQTTManagerDelegate>)delegate
{
    static DTCMQTTManager *static_manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        static_manager = [[DTCMQTTManager alloc]init];
        static_manager.delegate = delegate;
    });
    return static_manager;
}

- (instancetype)init
{
    if ([super init]) {
        [self session];
    }
    return self;
}

- (MQTTSession *)session
{
    if (!_session) {
        _session = [[MQTTSession alloc]init];
        _session.transport = self.transport;
    }
    return _session;
}

- (MQTTCFSocketTransport *)transport
{
    if (!_transport) {
        _transport = [[MQTTCFSocketTransport alloc]init];
    }
    return _transport;
}
@end
